<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\User;
use Hash;
use Session;

class UserController extends Controller
{
    //列表显示
    public function index(Request $request)
    {
    	$user = User::where(function($query) use ($request){

    		//检测参数
    		if(!empty($request->username)){
    			$query->where('username','like','%'.$request->username.'%');
    		}
    		//检测邮箱
    		if(!empty($request->email)){
    			$query->where('email',$request->email);
    		}
    		//检测手机号
    		if(!empty($request->phone)){
    			$query->where('phone',$request->phone);
    		}


    	})->orderBy('id','asc')->paginate($request->input('num',10));

    	return view('admin.user.index',['user'=>$user,'request'=>$request]);
    }

    public function create()
    {
    	//显示用户添加的表单
    	return view('admin.user.create');
    }

    public function store(Request $request)
    {
    	$this->validate($request,[
    		'username' => 'required',
    		'password' => 'required|regex:/^\S{6,30}$/|same:repassword',
    		'repassword' => 'required|regex:/^\S{6,30}$/|same:password',
    		'email' => 'required|regex:/^(\w)+@(\w)+((\.\w+)+)$/',
    		'phone' => 'required|regex:/^1[3578][0-9]{9}$/',
    	],[
    		'username.required' => '用户名不能为空',
    		// 'username.regex' => '用户名格式不正确',
    		'password.required' => '密码不能为空',
    		'password.regex' => '密码格式不正确',
    		'password.same' => '两次密码不一致',
    		'email.required' => '邮箱不能为空',
    		'email.regex' => '邮箱格式不正确',
    		'phone.required' => '电话号码不能为空',
    		'phone.regex' => '电话号码格式不正确',
    	]);

    	//实例化模型对象
    	$user = new User;
    	$user -> username = $request->username;
    	$user -> password = Hash::make($request->password);
    	$user -> email = $request->email;
    	$user -> phone = $request->phone;
    	$user -> sex = $request->sex;

    	//上传头像
    	if($request->hasFile('profile')){
    		//定义文件上传的路径
    		$dirname = './Uploads/'.date('Ymd').'/';
    		//获取文件的后缀名
    		$suffix = $request->file('profile')->getClientOriginalExtension();
    		//设置文件名随机
    		$name = time().rand(100000,999999).'.'.$suffix;
    		$request->file('profile')->move($dirname,$name);
    		//将文件存入到对象中(数据库之中)
    		$user ->profile =trim($dirname,'.').$name;
    	}

    	if($user->save()){
    		return redirect('/user')->with('success','执行成功');
    	}
    }

    public function edit($id)
    {
    	$user = User::findOrFail($id);

    	return view('admin.user.edit',['user'=>$user]);
    }

    public function update(Request $request,$id)
    {
    	$this->validate($request,[
    		'username' => 'required',
    		// 'password' => 'required|regex:/^\S{6,30}$/|same:repassword',
    		// 'repassword' => 'required|regex:/^\S{6,30}$/|same:password',
    		'email' => 'required|regex:/^(\w)+@(\w)+((\.\w+)+)$/',
    		'phone' => 'required|regex:/^1[3-8][0-9]{9}$/',
    	],[
    		'username.required' => '用户名不能为空',
    		// 'username.regex' => '用户名格式不正确',
    		// 'password.required' => '密码不能为空',
    		// 'password.regex' => '密码格式不正确',
    		// 'password.same' => '两次密码不一致',
    		'email.required' => '邮箱不能为空',
    		'email.regex' => '邮箱格式不正确',
    		'phone.required' => '电话号码不能为空',
    		'phone.regex' => '电话号码格式不正确',
    	]);

    	//读取内容
    	$user = User::findOrFail($id);

    	//提取内容
    	$user -> username = $request->username;
    	$user -> email = $request->email;
    	$user -> phone = $request->phone;
    	$user -> sex = $request->sex;

    	//上传头像
    	if($request->hasFile('profile')){
    		//定义文件上传的路径
    		$dirname = './Uploads/'.date('Ymd').'/';
    		//获取文件的后缀名
    		$suffix = $request->file('profile')->getClientOriginalExtension();
    		//设置文件名随机
    		$name = time().rand(100000,999999).'.'.$suffix;
    		$request->file('profile')->move($dirname,$name);
    		//将文件存入到对象中(数据库之中)
    		$user ->profile =trim($dirname,'.').$name;
    	}

    	if($user->save()){
    		return redirect('/user')->with('success','修改成功');
    	}else{
            return back()->with('error','修改失败');
        }
    	
    }

    public function destroy($id)
    {
    	$user = User::findOrFail($id);

    	if($user->delete()){
    		return back()->with('success','执行成功');
    	}else{
    		return back()->with('error','执行失败');
    	}
    }


}
